package com.cn.kehong.service;

import com.cn.kehong.api.IProductChuciPeriodService;
import com.cn.kehong.dao.ProductChuciPeriodMapper;
import com.cn.kehong.domain.ProductChuciPeriod;
import com.cn.kehong.domain.ProductChuciPeriodExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductChuciPeriodServiceImpl implements IProductChuciPeriodService {

    @Autowired
    private ProductChuciPeriodMapper productChuciPeriodMapper;

    @Override
    public List<ProductChuciPeriod> getProductChuciPeriodList(QueryForm queryForm) throws Exception {
        int count = productChuciPeriodMapper.selectProductChuCiCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductChuciPeriod> productThinPeriods = productChuciPeriodMapper.selectProductChuCis(queryForm);
        productThinPeriods.get(0).setCount(count);
        return productThinPeriods;
    }

    @Override
    public List<ProductChuciPeriod> getProductChuciPeriodListByCodes(List<String> productCodes) throws Exception {
        if(CollectionsUtil.isEmpty(productCodes)){
            return Lists.newArrayList();
        }
        ProductChuciPeriodExample example = new ProductChuciPeriodExample();
        example.createCriteria().andProductCodeIn(productCodes).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ProductChuciPeriod> productChuciPeriods = productChuciPeriodMapper.selectByExample(example);
        return productChuciPeriods;
    }


    @Override
    public String addProductChuciPeriod(String peiId,String productCode,String factoryName,String startCode) throws Exception {
        if(StringUtils.isEmpty(peiId)||StringUtils.isEmpty(productCode)||StringUtils.isEmpty(factoryName)){
            throw new Exception("peiId or productCode or factoryName is null");
        }
        ProductChuciPeriodExample example = new ProductChuciPeriodExample();
        example.createCriteria().andPeiIdEqualTo(peiId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = productChuciPeriodMapper.countByExample(example);
        if(existCount > 0){
            throw new Exception("data has exist");
        }
        ProductChuciPeriod productChuciPeriod = new ProductChuciPeriod();
        String id = CommonUtil.generateRandomNum("chuci-");
        productChuciPeriod.setId(id);
        productChuciPeriod.setFactoryName(factoryName);
        productChuciPeriod.setProductCode(productCode);
        productChuciPeriod.setPeiId(peiId);
        productChuciPeriod.setStartCode(startCode);
        int count = productChuciPeriodMapper.insertSelective(productChuciPeriod);
        if(count < 1){
            throw new Exception("insert chuci data error");
        }
        return "";
    }

    @Override
    public String updateBatchProductChuciPeriod(List<ProductChuciPeriod> productChuciPeriods) throws Exception {
        if(CollectionUtils.isEmpty(productChuciPeriods)){
            return "数组为空";
        }

        int count = productChuciPeriodMapper.batchUpdateChuciPeriods(productChuciPeriods);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductChuciPeriod> getProductChuciPeriodBackList(QueryForm queryForm) throws Exception {
        int count = productChuciPeriodMapper.getProductChuciPeriodBackCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductChuciPeriod> productThinPeriods = productChuciPeriodMapper.getProductChuciPeriodBackList(queryForm);
        productThinPeriods.get(0).setCount(count);
        return productThinPeriods;
    }

    @Override
    public String getLatestProductCode(String factoryName) {
        return productChuciPeriodMapper.selectLatestProductCode(factoryName);
    }

}
